2 * Copyright (c) 2017-2018 Apple Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "unittest_common.h"
18 #import <XCTest/XCTest.h>
20 // This DNS message was gleaned from a uDNS query request packet that was captured with Wireshark.
21 uint8_t udns_query_request_message[28] = { // contains 1 question for www.f5.com
22 0x31, 0xca, // transaction id
24 0x00, 0x01, // 1 question
25 0x00, 0x00, // no anwsers
26 0x00, 0x00, // no authoritative answers
27 0x00, 0x00, // no additionals
28 0x03, 0x77, 0x77, 0x77, 0x02, 0x66, 0x35, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01
31 // This DNS message was gleaned from a uDNS query request packet that was captured with Wireshark.
32 // Then the header id (more specifically, the msg->h.id) was deliberately cleared to force code
33 // path to traverse regression case, <rdar://problem/28556513>.
34 uint8_t udns_query_request_message_with_invalid_id[28] = { // contains 1 question for www.f5.com, msg->h.id = 0
35 0x00, 0x00, // transaction id
37 0x00, 0x01, // 1 question
38 0x00, 0x00, // no anwsers
39 0x00, 0x00, // no authoritative answers
40 0x00, 0x00, // no additionals
41 0x03, 0x77, 0x77, 0x77, 0x02, 0x66, 0x35, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01
44 uint8_t arp_request_packet[28] = { // contains 1 question for www.f5.com, msg->h.id = 0
45 0x00, 0x01, // hardware type: enet
46 0x08, 0x00, // protocol type: IP
47 0x06, // hardware size
49 0x00, 0x01, // opcode request
50 0x24, 0x01, 0xc7, 0x24, 0x35, 0x00, // Sender mac addr
51 0x11, 0xe2, 0x14, 0x01, // Sender ip addr
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // target mac addr
53 0x11, 0xe2, 0x17, 0xbe // target ip addr
56 mDNSlocal void InitmDNSStorage(mDNS *const m)
58 memset(m, 0, sizeof(mDNS));
61 @interface mDNSCoreReceiveTest : XCTestCase
66 @implementation mDNSCoreReceiveTest
70 mDNSPlatformTimeInit();
71 mDNS_LoggingEnabled = 0;
72 mDNS_PacketLoggingEnabled = 0;
78 - (void)testValidQueryReq
80 mDNS *const m = &mDNSStorage;
81 mDNSAddr srcaddr, dstaddr;
82 mDNSIPPort srcport, dstport;
86 // Init unit test environment and verify no error occurred.
87 mStatus result = init_mdns_environment(mDNStrue);
88 XCTAssertEqual(result, mStatus_NoError);
90 // Used random values for srcaddr and srcport
91 srcaddr.type = mDNSAddrType_IPv4;
92 srcaddr.ip.v4.b[0] = 192;
93 srcaddr.ip.v4.b[1] = 168;
94 srcaddr.ip.v4.b[2] = 1;
95 srcaddr.ip.v4.b[3] = 10;
96 srcport.NotAnInteger = swap16((mDNSu16)53);
98 // Used random values for dstaddr and dstport
99 dstaddr.type = mDNSAddrType_IPv4;
100 dstaddr.ip.v4.b[0] = 192;
101 dstaddr.ip.v4.b[1] = 168;
102 dstaddr.ip.v4.b[2] = 1;
103 dstaddr.ip.v4.b[3] = 20;
104 dstport.NotAnInteger = swap16((mDNSu16)49339);
106 // Set message to a DNS message (copied from a WireShark packet)
107 msg = (DNSMessage *)udns_query_request_message;
108 end = udns_query_request_message + sizeof(udns_query_request_message);
110 // Execute mDNSCoreReceive using a valid DNS message
111 mDNSCoreReceive(m, msg, end, &srcaddr, srcport, &dstaddr, dstport, if_nametoindex("en0"));
113 // Verify that mDNSCoreReceiveQuery traversed the normal code path
114 XCTAssertEqual(m->mDNSStats.NormalQueries, 1);
117 - (void)testNullDstQueryReqTest
119 mDNS *const m = &mDNSStorage;
121 mDNSIPPort srcport, dstport;
125 // This test case does not require setup of interfaces, the record's cache, or pending questions
126 // so m is initialized to all zeros.
129 // Used random values for srcaddr and srcport
130 srcaddr.type = mDNSAddrType_IPv4;
131 srcaddr.ip.v4.b[0] = 192;
132 srcaddr.ip.v4.b[1] = 168;
133 srcaddr.ip.v4.b[2] = 1;
134 srcaddr.ip.v4.b[3] = 10;
135 srcport.NotAnInteger = swap16((mDNSu16)53);
137 // Used random value for dstport
138 dstport.NotAnInteger = swap16((mDNSu16)49339);
140 // Set message to a DNS message (copied from a WireShark packet)
141 msg = (DNSMessage *)udns_query_request_message_with_invalid_id;
142 end = udns_query_request_message_with_invalid_id + sizeof(udns_query_request_message_with_invalid_id);
144 // Execute mDNSCoreReceive to regress <rdar://problem/28556513>
145 mDNSCoreReceive(m, msg, end, &srcaddr, srcport, NULL, dstport, if_nametoindex("en0"));
147 // Verify that mDNSCoreReceiveQuery was NOT traversed through the normal code path
148 XCTAssertEqual(m->mDNSStats.NormalQueries, 0);
150 // Verify code path that previously crashed, in <rdar://problem/28556513>, now traverses successfully
151 // by checking a counter that was incremented on code path that crashed.
152 XCTAssertEqual(m->MPktNum, 1);
156 - (void)testPerformanceExample {
157 // This is an example of a performance test case.
158 [self measureBlock:^{
159 // Put the code you want to measure the time of here.